package de.koller.aframe.plain;

import de.koller.oceanFramework.OceanException;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class ADisplayPlain extends Canvas {
	private static final long serialVersionUID = 1L;
	
	private final IARootPlain root;
	public final JFrame container;
	public final Container panel;
	private BufferStrategy strategy;
	public final int posX, posY, width, height;
	public boolean clearScreen = true;
	
	public ADisplayPlain( IARootPlain root, String title ) {
		Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
		this.root = root;
		this.posX = r.x;
		this.posY = r.y;
		this.width = r.width;
		this.height = r.height;
		
		this.container = new JFrame( title );
		this.container.setUndecorated( true );
		this.panel = container.getContentPane();
		
		this.panel.setPreferredSize( new Dimension( width, height ) );
		this.panel.setLayout( null );
		
		this.setBounds( 0, 0, width, height );
		this.panel.add( this );
		this.setIgnoreRepaint( true );
		
		this.container.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
		this.container.pack();
		this.container.setResizable( false );
		
		this.container.addWindowListener( new WindowAdapter() {
			@Override public void windowClosing( WindowEvent e ) {
				ADisplayPlain.this.root.displayClosed();
			}
		} );
		
	}
	
	public ADisplayPlain( IARootPlain root, int width, int height, String title ) {
		this.root = root;
		this.posX = -1;
		this.posY = -1;
		this.width = width;
		this.height = height;
		
		this.container = new JFrame( title );
		this.panel = container.getContentPane();
		
		this.panel.setPreferredSize( new Dimension( width, height ) );
		this.panel.setLayout( null );
		
		this.setBounds( 0, 0, width, height );
		this.panel.add( this );
		this.setIgnoreRepaint( true );
		
		this.container.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
		this.container.pack();
		this.container.setResizable( false );
		
		this.container.addWindowListener( new WindowAdapter() {
			@Override public void windowClosing( WindowEvent e ) {
				ADisplayPlain.this.root.displayClosed();
			}
		} );
		
	}
	
	public DisplayMode[] getDisplayModes() {
		GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		return graphicsDevice.getDisplayModes();
	}
	
	public void setFullscreen( DisplayMode mode ) {		
		GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		
		graphicsDevice.setFullScreenWindow( container );
		graphicsDevice.setDisplayMode( mode );
	}
	
	public void init() {
		container.setVisible( true );
		container.pack();
		
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		container.setLocation(	posX == -1 ? (int)(d.getWidth() / 2. - container.getWidth() / 2.) : posX,
								posY == -1 ? (int)(d.getHeight() / 2. - container.getHeight() / 2.) : posY );
		
		this.createBufferStrategy( 2 );
		strategy = this.getBufferStrategy();
		
		this.requestFocus();
	}
	
	public void release() {
		container.dispose();
	}
	
	public void render() {
		Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
	
		if( clearScreen ) {
			g.setColor( Color.BLACK );
			g.fillRect( 0, 0, width, height );
		}
		
		root.render( g );
		
		g.dispose();
		strategy.show();
	}
	
}
